/**
 * Copyright Statement:
 * This software and related documentation (ESWIN SOFTWARE) are protected under relevant copyright laws.
 * The information contained herein is confidential and proprietary to
 * Beijing ESWIN Computing Technology Co., Ltd.(ESWIN)and/or its licensors.
 * Without the prior written permission of ESWIN and/or its licensors, any reproduction, modification,
 * use or disclosure Software, and information contained herein, in whole or in part, shall be strictly prohibited.
 *
 * Copyright ©[2023] [Beijing ESWIN Computing Technology Co., Ltd.]. All rights reserved.
 *
 * RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES THAT THE SOFTWARE
 * AND ITS DOCUMENTATIONS (ESWIN SOFTWARE) RECEIVED FROM ESWIN AND / OR ITS REPRESENTATIVES
 * ARE PROVIDED TO RECEIVER ON AN "AS-IS" BASIS ONLY. ESWIN EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON INFRINGEMENT.
 * NEITHER DOES ESWIN PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE SOFTWARE OF ANY THIRD PARTY
 * WHICH MAY BE USED BY,INCORPORATED IN, OR SUPPLIED WITH THE ESWIN SOFTWARE,
 * AND RECEIVER AGREES TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL ESWIN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * @file main.c
 * @brief Examples for Arduino Port usage.
 * @author abu (abu@eswincomputing.com)
 * @date 2023-12-13
 *
 * @details
 *
 * @platform Fullmask evb board and Arduino board. link:
 * https://item.taobao.com/item.htm?spm=a21n57.1.0.0.4b0e523coCgv03&id=709193578380&ns=1&abbucket=1#detail
 *
 * @pins
 *
 * @connections
 * Arduino Expansion Board is plugged into Arduino Port J13
 * And VCC in Arduino Expansion Board is connected to 3.3V in ESWIN board
 *
 * Modification History :
 * Date:               Version:                    Author:
 * Changes:
 *
 */

#include "../board/sdk_project_config.h"
#include "osal.h"
#include "arduino.h"

/** Arduino Example finish message displayed at the console */
#define LED_MSG "Arduino LED Example finish \r\n"
#define BUZZER_MSG "Arduino Buzzer Example finish \r\n"
#define DHT11_MSG "Arduino DHT11 Example finish \r\n"
#define PWM_MSG "Arduino PWM Example finish \r\n"


// 宏定义
#define DHT11_CHECK_INTERVAL_MS 2000  // DHT11检测间隔(2秒)
#define TEMP_THRESHOLD_LOW      25     // 低温阈值
#define TEMP_THRESHOLD_HIGH     30     // 高温阈值

/**
 * @brief The main function for the project.
 */
int main(void)
{
    /* Clock Init */
    CLOCK_SYS_Init(g_pstClockManConfigsArr[0]);
    /** Initialize pins
     *    -    See PinSettings component for more info
     */
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_stPinmuxConfigArr);
    /* Initialize UART instance */
    UART_DRV_Init(INST_UART_2, &g_stUartState_2, &g_stUart2UserConfig0);

    /* Set the serial port for printf*/
    setLogPort(2);
    printf("Arduino Begin: \r\n");
    while (1){
    	if (CONFIG_DHT11) {
			int temp = Arduino_Dht11_Example();
			printf("%d", temp);
			if (temp < 25){
				PINS_DRV_WritePin(PORTB, 30, 0);
			}
			else if (temp < 30){
				PINS_DRV_WritePin(PORTB, 30, 1);
				g_stPwm1Config0.duty = 0xffff;
				PWM_DRV_Cfg(INST_PWM_1, &g_stPwmState_1, &g_stPwm1Config0);
				PWM_DRV_Deinit(INST_PWM_1);
				OS_DelayMs(100);
			}
			else if(temp < 35){
				Arduino_Buzzer_Example();
				PINS_DRV_WritePin(PORTB, 30, 1);
				g_stPwm1Config0.duty = 170;
				PWM_DRV_Cfg(INST_PWM_1, &g_stPwmState_1, &g_stPwm1Config0);
				OS_DelayMs(100);
			}
			else {
				Arduino_Buzzer_Example();
				PINS_DRV_WritePin(PORTB, 30, 1);
				g_stPwm1Config0.duty = 0;
				PWM_DRV_Cfg(INST_PWM_1, &g_stPwmState_1, &g_stPwm1Config0);
				OS_DelayMs(100);
			}
			printf("%s", DHT11_MSG);
		}
    	PWM_DRV_Deinit(INST_PWM_1);
    }

    printf("Arduin End!!! \r\n");
}
